﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using LinqToBing.Bing.Wrapper;
using LinqToBing.Bing.Wrapper.Enums;
using LinqToBing.Bing.Wrapper.Search;
using System.Configuration;

namespace LinqToBing.Winform.Test
{
    public partial class frmLinqToBingTest : Form
    {
        public frmLinqToBingTest()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SearchHelper.RegisterAppId(ConfigurationManager.AppSettings["AppId"]);

            int iStep = 80;
            int iLeft = -iStep;
            Enum.GetValues(typeof(SearchSource)).Cast<SearchSource>().ToList<SearchSource>().
                ForEach(item =>
                {
                    pnlSearchTypes.Controls.Add(
                        new CheckBox()
                        {
                            Text = item.ToString(),
                            Left = iLeft = iLeft + iStep,
                            Width = iStep,
                            TextAlign = ContentAlignment.MiddleLeft,
                            Tag = item
                        }
                        );
                }
                );




            //LinqToBing.Bing.Linq.LiveSearchContext ctx = new Bing.Linq.LiveSearchContext(AppId);
            //var Result = ctx.Web.Where(set => set.Query.Equals("Prova Bing"));

            //Result.ToList<LiveSearchResultWeb>().ForEach(result =>
            //    {
            //        result.XDocument.ToString();
            //    });

            //ctx.Web().Where(set=>set.Qu)
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<SearchTypeContainer> sc = new List<SearchTypeContainer>();
            pnlSearchTypes.Controls.OfType<CheckBox>()
               .Where(chb => chb.Checked==true)
               .ToList<CheckBox>()
               .ForEach(
               chb =>
               {
                   sc.Add(SearchTypeContainer.CreateContainer((SearchSource)chb.Tag));
               });
            //SearchTypeContainer[] stc =
            //    new SearchTypeContainer[]{ 
            //        SearchTypeContainer.CreateContainer(), 
            //        SearchTypeContainer.CreateContainer(SearchTypeEnum.Image)
            //    };
            SearchTypeContainer[] stc = sc.ToArray<SearchTypeContainer>();
            SearchRequest search = new SearchRequest()
                { 
                    AppId = ConfigurationManager.AppSettings["AppId"],
                    SearchProtocol = SearchProtocol.JSON,
                    Query = txtSearchQuery.Text,
                    Searches = sc
                };
            //SearchResult result = SearchHelper.ExecuteSearch(search);
            //XDocument xdoc = result.XDocument;
            XDocument xdoc = SearchHelper.ExecuteSearch(search).XDocument; // DoSearch(stc, txtSearchQuery.Text);

            richTextBox1.Text = xdoc.ToString();
            xdoc.Save("prova.xml");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<SearchTypeContainer> sc = new List<SearchTypeContainer>();
            pnlSearchTypes.Controls.OfType<CheckBox>()
               .Where(chb => chb.Checked == true)
               .ToList<CheckBox>()
               .ForEach(
               chb =>
               {
                   sc.Add(SearchTypeContainer.CreateContainer((SearchSource)chb.Tag));
               });
            //SearchTypeContainer[] stc =
            //    new SearchTypeContainer[]{ 
            //        SearchTypeContainer.CreateContainer(), 
            //        SearchTypeContainer.CreateContainer(SearchTypeEnum.Image)
            //    };
            SearchTypeContainer[] stc = sc.ToArray<SearchTypeContainer>();
            SearchRequest search = new SearchRequest()
            {
                AppId = ConfigurationManager.AppSettings["AppId"],
                SearchProtocol = SearchProtocol.JSON,
                Query = txtSearchQuery.Text

            };
            SearchResult result = SearchHelper.ExecuteSearch(search);
            XDocument xdoc = result.XDocument;
            //DoSearch(stc, txtSearchQuery.Text);
            richTextBox1.Text = xdoc.ToString();
            xdoc.Save("prova.xml");

        }
    }
}
